package com.tdx.blog_nowcoder.utils;

import java.util.ArrayList;
import java.util.List;

// 做分页查询处理
public class Page {


    private int currentPage = 1;
    private int perPageSize = 10;
    //总记录数
    private int totalPageNums;
    //给哪一个路径配置分页
    private String path;


    private int total;

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        if(currentPage > 1){
            this.currentPage = currentPage;
        }
    }

    public int getPerPageSize() {
        return perPageSize;
    }

    public void setPerPageSize(int perPageSize) {
        if(perPageSize>1&&perPageSize<50){
            this.perPageSize = perPageSize;
        }
    }

    public int getTotalPageNums() {
        return totalPageNums;
    }

    public void setTotalPageNums(int totalPageNums) {
        if(totalPageNums>=0){
            this.totalPageNums = totalPageNums;
        }

    }

    public String getPath() {
//        List<List<Integer>> list = new ArrayList<>();
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    /**
     * 获取当前页的起始行
     *
     * @return
     */
    public int getOffset() {
        // current * limit - limit
        return (currentPage - 1) * perPageSize;
    }

    //总页数
    public int getTotal(){
        // rows / limit [+1]
        if (totalPageNums % perPageSize == 0) {
            return totalPageNums / perPageSize;
        } else {
            return totalPageNums / perPageSize + 1;
        }
    }

    public void setTotal(int total) {
        this.total = total;
    }



    /**
     * 获取起始页码
     *
     * @return
     */
    public int getFrom() {
        int from = currentPage - 2;
        return from < 1 ? 1 : from;
    }

    /**
     * 获取结束页码
     *
     * @return
     */
    public int getTo() {
        int to = currentPage + 2;
        int total = getTotal();
        return to > total ? total : to;
    }

}
